<?php
namespace app\test\controller;
use app\test\controller\Base;
use app\test\model\city;
use app\test\model\tushu_setting;
use think\Request;
use think\Validate;

class Index extends Base{

    //用户列表
    public function index()
    {
//        echo '<pre>';
//        print_r(session('username'));
//        die;
        $keyword = input('keyword');
        $map = [];
        if($keyword) $map['title|addr'] = ['like', '%'.$keyword.'%'];
        $tushu = tushu_setting::where($map)->order('id desc')->paginate(8);
        $page = $tushu->render();
        $this->assign('tushu', $tushu);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //数据添加
    public function insert()
    {
        if(Request()->isPost()){
            $post = input('post.');
            $check = $this->Validate($post, 'TushuSetting');
            if(true !== $check) $this->error($check);

            $data = [
                'title' => $post['title'],
                'addr' => $post['addr'],
                'tel' => $post['tel'],
                'create_time' => time(),
            ];
            $tushu = tushu_setting::insert($data);
            if(!$tushu){
                $this->error('数据添加失败！');
            } else {
                $this->success('数据添加成功！','index');
            }
        }
        return $this->fetch();
    }

    //数据更新
    public function update()
    {
        if(Request()->isPost()){
            $post = input('post.');
            $check = $this->Validate($post, 'TushuSetting');
            if(true !== $check) $this->error($check);

            $data = [
                'title' => $post['title'],
                'addr' => $post['addr'],
                'tel' => $post['tel'],
                'update_time' => time(),
            ];
            $res = tushu_setting::where('id', $post['id'])->update($data);
            if(!$res){
                $this->error('数据更新失败！');
            } else {
                $this->success('数据更新成功！', 'index');
            }
        }
        $id = input('id');
        $info = tushu_setting::where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    //数据删除
    public function delete()
    {
        $id = input('id');
        $res = tushu_setting::where('id', $id)->delete();
        if(!$res){
            $this->error('删除失败！');
        } else {
            return Json(['msg' => '删除成功！', 'status' => 1]);
        }
        return $this->fetch();
    }


    //无线分类
    public function test1()
    {

        $model = new city();
        $list = $model->select();
        $lists = $model->getTree($list);
        $this->assign('list', $lists);
        return $this->fetch();
    }

}

